package com.test.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.test.pojo.TbAddress;
import com.test.pojo.TbCart;
import com.test.pojo.TbOrder;
import com.test.pojo.TbOrderItem;
import com.test.service.TbAddressService;
import com.test.service.TbCartService;
import com.test.service.TbOrderItemService;
import com.test.service.TbOrderService;
import com.test.vo.AddressVO;
import com.test.vo.ToOrderItemVO;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 * @author yanglijun
 * @since 2020-12-12
 */
@RestController
@RequestMapping("/tb-address")
public class TbAddressController {
    @Resource
    TbAddressService addressService;
    @Resource
    TbCartService cartService;
    @Resource
    TbOrderService orderService;
    @Resource
    TbOrderItemService orderItemService;
    //点击提交订单进行（未付款状态的订单生成）
    //单个购买的人先把东西添加到购物车列表
    @RequestMapping("/addOrder")
    public String addOrder( @RequestParam String userName,
                            @RequestParam String buyerMessage,
                            @RequestParam String contact,
                            @RequestParam String mobile,
                            @RequestParam String address
                            ){
      //生成订单号  private String id;
       String orderId= UUID.randomUUID().toString();

       /**
         * id，total_num，total_money，pre_money优惠金额，
         * post_fee邮费，pay_money实付金额，create_time创建时间，
         * update_time订单更新时间，pay_time付款时间，
         * username用户名称，buyer_message买家留言，
         * receiver_contact收货人，receiver_mobile收货人电话，receiver_address地址，
         * order_status订单状态，pay_status支付状态，consign_status发货状态 0:未发货 1:已发货 2:已送达，
         * is_delete是否删除
         */
            //邮费固定12
        Integer postFee=12;
        //pay_money实付金额=总价-优惠
        Integer payMoney=0;
        //create_time创建时间，
        Date createDate = new Date();

        //username用户名称，前面传过来

       // buyer_message买家留言，前面传过来

        //receiver_contact收货人，receiver_mobile收货人电话，receiver_address地址，

        //order_status订单状态给0未完成，pay_status支付状态给0:未支付，consign_status发货状态 给0:未发货

        //is_delete是否删除 给0

        //跟商品有关的信息都要从购物车列表的提交字段为1的查出来获得信息
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("isSurplus",1);
        List<TbCart> goodsList = cartService.list(queryWrapper);
        Integer totalSum = 0;
        Integer totalMoney=0;
        Integer preMoney=0;
        for (TbCart good:goodsList) {
            totalSum+= good.getTotalNum();
            totalMoney+=good.getSubtotalMoney();
            preMoney+=good.getPreMoney();
        }
        TbOrder order =new TbOrder(orderId,totalSum,totalMoney,preMoney,
                12,totalMoney-preMoney,"1",createDate,
                createDate,null,null,null,
                null,null,null,
                userName,buyerMessage,"0",contact,
                mobile,address,"1",null,
                "0","0","0","0");
        //进行最初的订单数据添加
        orderService.save(order);
        //下面是orderItem表单的添加
        /**
         * tb_order_item
         * id（没什么用），category_id1，category_id2，category_id3，123级分类
         * spu_id，（spu表用商品id查）
         * sku_id，
         * order_id 订单id，NAME 商品名称，（spuName）
         * price单价，num数量，money总金额，pay_money实付金额，（单类商品的并不是整个订单）
         * image图片，post_fee邮费，is_return 是否退货0未退货 1已退货
         */
        //id先放着吧
        //category_id1，category_id2，category_id3,spu_id,spu表用商品id查
        List<ToOrderItemVO>toOrderItemVOS=addressService.getToOrderItem();

        for (int i = 0; i <toOrderItemVOS.size() ; i++) {
            ToOrderItemVO orderItemVO=toOrderItemVOS.get(i);
            String id=orderId+i;
            Integer categoryId1=orderItemVO.getCategory1Id();
            Integer categoryId2=orderItemVO.getCategory2Id();
            Integer categoryId3=orderItemVO.getCategory3Id();
            String spuId=orderItemVO.getId();
            String skuId=null;
            String name=orderItemVO.getName();
            Integer price=orderItemVO.getPrice();
            Integer num=orderItemVO.getTotalNum();
            Integer money=orderItemVO.getSubtotalMoney();
            //看情况写不写优惠金额吧
           // payMoney=money-orderItemVO.getPreMoney();
            String image=orderItemVO.getImage();
            Integer weight=15;
            String isReturn="0";
            TbOrderItem orderItem=new TbOrderItem(id,categoryId1,
                    categoryId2,categoryId3,spuId,skuId,orderId,
                    name,price,num,money,money,image,weight,
                    postFee,isReturn);
             orderItemService.save(orderItem);
        }
        //清空所有 已经提交结算的购物车商品
        //TODO:先注释掉删除的避免测试数据失效
        cartService.remove(queryWrapper);

        return orderId;
    }

    //拿到要结算的商品清单
    @RequestMapping("/getGoodsList")
    public List<TbCart> getGoodsList(String userName){
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("userName","lijialong");
    return  cartService.list(queryWrapper);
    }

    //拿到当前用户的地址信息
    @RequestMapping("/getAddressList")
    public List<AddressVO> getAddressList(String userName){
        return addressService.getAddressList(userName);
    }
}

